const webpack = require('webpack');
const { ModuleFederationPlugin } = require('webpack').container;
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

const exposeConfig = require('./exposes/config.json');

/** 随机字符串 */
const randomString = (n = 2) => {
  const CHAR_LIST = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
  let res = '';
  for (let i = 0; i < n; i++) {
    res += CHAR_LIST[Math.floor(Math.random() * CHAR_LIST.length)];
  }
  return res;
};
module.exports = {
  publicPath: '/extensions/route',
  // publicPath: '.',
  productionSourceMap: false,
  configureWebpack: (config) => {
    config.output.uniqueName = randomString(6);
    if (process.env.NODE_ENV === 'production') {
      // 因为vue组件需要使用同一个vue实例，二开组件中的vue不能单独引用
      // config.externals = {
      //   vue: 'Vue',
      // };
      // 只需打包组件，重设entry
      config.entry = './entry-exposes.ts';
      config.plugins.push(new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn/));
      // config.plugins.push(new BundleAnalyzerPlugin());

      const getShared = () => {
        const res = {};
        const sharedList =  [
          '@h3/antd-vue'
        ]
        sharedList.forEach(key => {
          res[key] = {
            // import: false,
            // packageName: key,
            shareScope: 'default',
            shareKey: key,
            singleton: true
          }
        })
        return res;
      }

      config.plugins.push(
        new ModuleFederationPlugin({
          name: exposeConfig.name,
          filename: 'remoteEntry.js',
          library: { type: 'var', name: exposeConfig.name },
          exposes: exposeConfig.exposes,
          shared: getShared()
        }),
      );
    }
  },
  css: {
    extract:
      process.env.NODE_ENV === 'production'
        ? {
            ignoreOrder: true,
          }
        : false,
    loaderOptions: {
      less: {
        modifyVars:{},
        javascriptEnabled: true,
      },
    },
  },
};